package server.core;

import server.*;


public class StandardHostMapper implements Mapper {
    private StandardHost host = null;
    private String protocol = null;
    @Override
    public Container getContainer() {
        return host;
    }

    @Override
    public void setContainer(Container container) {
        if (!(container instanceof StandardHost))
            throw new IllegalArgumentException("StandardHostMapper必须与Host容器关联");
        host = (StandardHost) container;
    }

    @Override
    public String getProtocol() {
        return protocol;
    }

    @Override
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    @Override
    public Container map(Request request, boolean update) {
        String uri = ((HttpRequest)request).getDecodedRequestURI();
        Context context = host.map(uri);
        request.setContext(context);
        if (context != null) {
            ((HttpRequest) request).setContextPath("/" + context.getName());
        } else {
            ((HttpRequest) request).setContextPath(null);
        }
        return context;
    }
}
